package com.zp.crm.service.MenuService.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zp.crm.dao.MenuDao.MenuDao;
import com.zp.crm.dao.RoidDao.RoidDao;
import com.zp.crm.dao.UserDao.UserDao;
import com.zp.crm.dto.Menu.MenuDto;
import com.zp.crm.dto.Roid.RoidDto;
import com.zp.crm.dto.User.UserDto;
import com.zp.crm.entity.Menu.MenuEntity;
import com.zp.crm.entity.Roid.RoidEntity;
import com.zp.crm.entity.User.UserEntity;
import com.zp.crm.service.MenuService.MenuService;
import com.zp.crm.util.targetLog.Log;
@Service("menuService")
public class MenuServiceImpl implements MenuService {

	@Resource
	public UserDao userDao;
	@Resource
	public MenuDao menuDao;
	@Resource
	public RoidDao roidDao;
	@Log(name="根据用id查询权限输，并封装成Html页面形式 ")
	@Override
	public Map<String, Object> selectBykeyUserMenuList(String u_id,String html) {
		Map<String, Object> map=new HashMap<String,Object>();
		if(u_id != null && u_id.length()!=0){
			String user_roid=userDao.selectBykeyUserRoid(u_id);
			if(user_roid != null && !user_roid.equals("")){
				String[] obj=user_roid.split(",");
				List<Integer> userInt=new ArrayList<Integer>();
				for(int i=0;i<obj.length;i++){
					userInt.add(Integer.parseInt(obj[i]));
				}
				List<RoidEntity> roidlist=roidDao.selectBykeyUserRoidList(userInt);
				List<Integer> menuid=new RoidDto().RoidMenuToString(roidlist);
				List<MenuEntity> menulist=menuDao.selectByKeyInRoid(menuid);
				String menutext=new MenuDto().MenuList(menulist,html);
				map.put("success", true);
				map.put("message", menutext);
			}else{
				map.put("success", false);
				map.put("message", "您没有权限！");
			}
		}else{
			map.put("success", false);
			map.put("message", "请登录！");
		}
		return map;
	}
	@Log(name="查询用户是否拥有此角色")
	@Override
	public boolean selectIndexofRoid(String u_id,String name){
		boolean t=true;
		if(u_id != null && u_id.length()!=0){
			String user_roid=userDao.selectBykeyUserRoid(u_id);
			if(user_roid != null && !user_roid.equals("")){
				String[] obj=user_roid.split(",");
				List<Integer> userInt=new ArrayList<Integer>();
				for(int i=0;i<obj.length;i++){
					userInt.add(Integer.parseInt(obj[i]));
				}
				List<RoidEntity> roidlist=roidDao.selectBykeyUserRoidList(userInt);
				t=new RoidDto().RoidMenuNameText(roidlist,name);
			}else{
				t=false;
			}
		}else{
			t=false;
		}
		return t;
	}
	@Log(name="指定功能模块查询 ")
	@Override
	public Map<String, Object> menuZDCreate(String roidname) {
		Map<String, Object> map=new HashMap<>();
		
		if(roidname != null && roidname.length()>0){
			RoidEntity code=roidDao.selectBykeyCodeName(roidname);
			
			String c=String.valueOf(code.getId());
			
			List<UserEntity> list=userDao.selectBykeyRoid(c);
			
			map.put("success", true);
			
			map.put("message", new UserDto().SelectOption(list));
		}else{
			map.put("success", false);
		}
		
		return map;
	}
	@Log(name="添加功能模块")
	@Override
	public Map<String, Object> insert(MenuEntity entity) {
		Map<String, Object> map=new HashMap<>();

		int r=menuDao.insert(entity);
		
		if(r>0){
			map.put("success",true);
			map.put("message","添加数据成功！");
		}else{
			map.put("success",false);
			map.put("message","添加数据失败！");
		}
		
		return map;
	}
	@Log(name="删除功能模块 ")
	@Override
	public Map<String, Object> delete(int id) {
		Map<String, Object> map=new HashMap<>();

		int r=menuDao.delete(id);
		
		if(r>0){
			map.put("success",true);
			map.put("message","删除数据成功！");
		}else{
			map.put("success",false);
			map.put("message","删除数据失败！");
		}
		return map;
	}
	@Log(name="修改功能模块 ")
	@Override
	public Map<String, Object> update(MenuEntity entity) {
		Map<String, Object> map=new HashMap<>();

		int r=menuDao.update(entity);
		
		if(r>0){
			map.put("success",true);
			map.put("message","修改数据成功！");
		}else{
			map.put("success",false);
			map.put("message","修改数据失败！");
		}
		return map;
	}
	@Log(name="根据Id查询功能模块信息 ")
	@Override
	public Map<String, Object> selectById(int id) {
		Map<String, Object> map=new HashMap<>();
		MenuEntity entity=menuDao.selectById(id);
		if(entity !=null){
			map.put("success",true);
			map.put("message",entity);
		}else{
			map.put("success",false);
			map.put("message","查询数据失败！");
		}
		return map;
	}
	
}
